package com.cuhk.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuhk.gulimall.product.entity.BrandEntity;
import com.cuhk.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cuhk.gulimall.product.entity.CategoryBrandRelationEntity;
import com.cuhk.gulimall.product.service.CategoryBrandRelationService;
import com.cuhk.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author chenyuijing
 * @email torchsam@aliyun.com
 * @date 2024-06-10 12:02:34
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 获取当前品牌关联的所有分类
     * Get请求
     * @param brandId
     * @return
     */
    @GetMapping("/catelog/list")
    public R list(@RequestParam Long brandId){
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(wrapper);
        return R.ok().put("data", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * 根据传入的三级分类的id
     * 返回这个分类下所有的品牌
     * 包括品牌id和品牌名字
     * @param catId
     * @return
     */
    @GetMapping("/brands/list")
        public R relationBrandsList(@RequestParam(value = "catId", required = true) Long catId ) {
        List<BrandEntity> brands = categoryBrandRelationService.getBrandByCatId(catId);
        List<BrandVo> res = brands.stream().map((brand) -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(brand.getBrandId());
            brandVo.setBrandName(brand.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", res);
    }

}
